jQuery(function($) {
	loadData();
});
function loadPlayerData(agentId){
	
	parent.window.Index.addTab(ctx + "/admin/usermanager/myPlayer?agentId="+agentId,"【"+agentId+"】下属玩家");

}
function changeChongzhiNum(obj){
	obj.value=obj.value.replace(/[^\d]/g,'') 
	var count = obj.value;
	if(count == "" || count == "0"){
		parent.window.$("#promptTxt").html("请输入正确的金币数量");
	}else{
		parent.window.$("#promptTxt").html("你好，您将给"+parent.window.$("#chongzhiDialog_userId").html()+"账户充值"+count+"个金币");
	}
}
function opeanChargeDialog(){
	
	
	var obj = $("input[type=radio]:checked");
	if(obj.length == 0){
		parent.window.$.gritter.add({
			title: '请选择要充值的代理',
			class_name: 'gritter-error',time: 2e3
		});
		return;
	}
	var userId = obj.attr("userId");
	var goldCoinCount = obj.attr("goldCoinCount");
	$("#chongzhiDialog_userId").html(userId);
	$("#chongzhiDialog_goldCoinCount").html(goldCoinCount);
	$("#promptTxt").html("你好，您将给"+userId+"账户充值1个金币");
	$("#chongzhi_num").attr("onkeyup",window.frameElement.id+".contentWindow.changeChongzhiNum(this)")
	
	$("#charge_confrimDialog #avatar").attr("src",obj.attr("userImg"))
	$("#charge_confrimDialog #confrim_nickName").html(obj.attr("userId")+"("+obj.attr("nickName")+")");
	$("#charge_confrimDialog #confrim_father").html(obj.attr("groupId"));
	$("#charge_confrimDialog #confrim_now").html(goldCoinCount);
	var charge_confrimDialog = $("#charge_confrimDialog");
	
	parent.window.Index.dialog({
		resizable: false,
		dialogClass: "no-close",
		width: '620',
		context:$("#chongzhiDialog").html(),
		modal: true,
		title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-rmb yellow'></i> 玩家充值</h4></div>",
		title_html: true,
		buttons: [
			{
				html: "<i class='ace-icon glyphicon glyphicon-ok bigger-110'></i>&nbsp; 保存",
				"class" : "btn btn-primary btn-minier",
				click: function() {
					var count = parent.window.$("#chongzhi_num").val();
					if(count == "" || count == "0"){
						return;
					}
					parent.window.Index.dialog({
						resizable: false,
						dialogClass: "no-close",
						width: '420',
						context:charge_confrimDialog.html(),
						modal: true,
						title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-rmb yellow'></i> 玩家充值 - 确认信息</h4></div>",
						title_html: true,
						buttons:[{
							html: "<i class='ace-icon glyphicon glyphicon-ok bigger-110'></i>&nbsp; 确定",
							"class" : "btn btn-primary btn-minier",click: function() {
								$.ajax({
									type:"POST",
									url:ctx+"/admin/usermanager/chargeJB",
									data:"userId="+userId+"&count="+count,
									cache:false,
									success:function(data){
										refreshPage();
									}
								});
								parent.window.$( this ).dialog( "close" );
							}
							},
							{html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 取消",
								"class" : "btn btn-minier",click: function() {
									parent.window.$( this ).dialog( "close" );
								}}]
					});
					parent.window.$("#confrim_thistime").html(count);
				}
			}
			,
			{
				html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 取消",
				"class" : "btn btn-minier",
				click: function() {
					parent.window.$( this ).dialog( "close" );
				}
			}
		]
	});
}

function openAgentDialog(){
	
	parent.window.Index.dialog({
		resizable: false,
		dialogClass: "no-close",
		width: '620',
		context:$("#agentDialog").html(),
		modal: true,
		title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-rmb yellow'></i> 设置代理</h4></div>",
		title_html: true,
		buttons: [
			{
				html: "<i class='ace-icon glyphicon glyphicon-ok bigger-110'></i>&nbsp; 保存",
				"class" : "btn btn-primary btn-minier",
				click: function() {
					var userid = parent.window.$("#setagent_userid").val();
					var agentid = parent.window.$("#setagent_agentid").val();
					if(userid == "" ){
						parent.window.$("#promptTxt").html("请输入要设置代理的玩家ID");
						return;
					}
					if(agentid == ""){
						parent.window.$("#promptTxt").html("请输入要设为代理玩家的上级代理ID或上级代理邀请码");
						return;
					}
					var userLevel = parent.window.$("#setagent_userLevel").val();
					var finalThis = this;
					$.ajax({
						type:"POST",
						url:ctx+"/admin/usermanager/createAgent",
						
						data:"userId="+userid+"&agentId="+agentid+"&userLevel="+userLevel,
						cache:false,
						dataType:"json",
						success:function(data){
							if(data.code == 1){
								parent.window.$(finalThis).dialog( "close" );
								parent.window.Index.dialog({
									resizable: false,
									dialogClass: "no-close",
									width: '620',
									context:"设置成功",
									modal: true,
									title: "<div class='widget-header'><h4 class='smaller'> 提示</h4></div>",
									title_html: true,
									buttons: [{html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 确定",
										"class" : "btn btn-primary btn-minier",click: function() {
											parent.window.$( this ).dialog( "close" );
										}}]});
								refreshPage();
							}else{
								parent.window.$("#promptTxt").html(data.msg);
							}
						}
					});
				}
			}
			,
			{
				html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 取消",
				"class" : "btn btn-minier",
				click: function() {
					parent.window.$( this ).dialog( "close" );
				}
			}
		]
	});
}
function loadData(){
	$.ajax({
		type:"POST",
		url:dataUrl,
		data:$("#agentQueryForm").serialize(),
		cache:false,
		success:function(data){
			$("#context").html(data);
		}
	});
}
var nextAgent = function(agentId){
	$("#queryInputStr").val("");
	$("#queryInputAgentId").val(agentId);
	loadData();
}
var returnTop = function(){
	$('#queryInputAgentId').val('');
	refreshPage();
}
function openUnAgentDialog(){
	var unagent_confrimDialog = $("#unagent_confrimDialog");
	
	parent.window.Index.dialog({
		resizable: false,
		dialogClass: "no-close",
		width: '620',
		context:$("#unAgentDialog").html(),
		modal: true,
		title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-rmb yellow'></i> 解除代理</h4></div>",
		title_html: true,
		buttons: [
			{
				html: "<i class='ace-icon glyphicon glyphicon-ok bigger-110'></i>&nbsp; 保存",
				"class" : "btn btn-primary btn-minier",
				click: function() {
					var userid = parent.window.$("#unsetagent_userid").val();
					if(userid == "" ){
						parent.window.$("#promptTxt1").html("请输入要解除代理的玩家ID");
						return;
					}
					var finalThis = this;
					$.ajax({
						type:"POST",
						url:ctx+"/admin/usermanager/confrimDeleteAgent",
						
						data:"userId="+userid,
						cache:false,
						dataType:"json",
						success:function(data){
							if(data.code == 1){
								unagent_confrimDialog.find("#unagent_user").html(data.data.userInfo.userId+"("+data.data.userInfo.nickName+")");
								unagent_confrimDialog.find("#unagent_userAgentId").html(data.data.userInfo.agentId);
								if(data.data.adminUser && data.data.adminUser.account){
									unagent_confrimDialog.find("#unagent_user_admin").html(data.data.adminUser.account+"(【"+data.data.adminUser.roleName+"】"+data.data.adminUser.name+")");
								}else{
									unagent_confrimDialog.find("#unagent_user_admin").html("无绑定后台用户");
								}
								unagent_confrimDialog.find("#unagent_user_father").html(data.data.groupUser.userId+"("+data.data.groupUser.nickName+")");
								unagent_confrimDialog.find("#unagent_avatar").attr("src",data.data.userInfo.userImg);
								unagent_confrimDialog.find("#unagent_user_father_agentId").html(data.data.groupUser.agentId);
								
								parent.window.Index.dialog({
									resizable: false,
									dialogClass: "no-close",
									width: '420',
									context:unagent_confrimDialog.html(),
									modal: true,
									title: "<div class='widget-header'><h4 class='smaller'>确认要解除以下玩家的代理吗？</h4></div>",
									title_html: true,
									buttons:[{
										html: "<i class='ace-icon glyphicon glyphicon-ok bigger-110'></i>&nbsp; 确定",
										"class" : "btn btn-primary btn-minier",click: function() {
											$.ajax({
												type:"POST",
												url:ctx+"/admin/usermanager/deleteAgent",
												data:"userId="+userid,
												cache:false,
												success:function(data){
													if(data.code != "0"){
														parent.window.Index.dialog({
															resizable: false,
															dialogClass: "no-close",
															width: '620',
															context:"设置成功",
															modal: true,
															title: "<div class='widget-header'><h4 class='smaller'> 提示</h4></div>",
															title_html: true,
															buttons: [{html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 确定",
																"class" : "btn btn-primary btn-minier",click: function() {
																	parent.window.$( this ).dialog( "close" );
																}}]});
													}else{
														parent.window.Index.dialog({
															resizable: false,
															dialogClass: "no-close",
															width: '620',
															context:data.msg,
															modal: true,
															title: "<div class='widget-header'><h4 class='smaller'> 失败</h4></div>",
															title_html: true,
															buttons: [{html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 确定",
																"class" : "btn btn-primary btn-minier",click: function() {
																	parent.window.$( this ).dialog( "close" );
																}}]});
													}
													
												}
											});
											parent.window.$( this ).dialog( "close" );
										}
										},
										{html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 取消",
											"class" : "btn btn-minier",click: function() {
												parent.window.$( this ).dialog( "close" );
											}}]
								});
							}else{
								parent.window.$("#promptTxt1").html(data.msg);
							}
						}
					});
				}
			}
			,
			{
				html: "<i class='ace-icon glyphicon glyphicon-remove bigger-110'></i>&nbsp; 取消",
				"class" : "btn btn-minier",
				click: function() {
					parent.window.$( this ).dialog( "close" );
				}
			}
		]
	});
}